import requests
from api.xbot.xbot_base_api import XbotBaseApi
class UserApi(XbotBaseApi):
 
    # ==================== User 微信号管理模块 ====================
    def user_bind_wechat(self, wxid: str, phone: str = "", token: str = ""):
        """绑定微信"""
        body = {
            "Wxid": wxid,
            "Phone": phone,
            "Token": token
        }
        return requests.post(
            f"{self.base_url}/User/BindWechat",
            json=body
        ).json()

    def user_get_login_status(self, wxid: str):
        """获取登录状态"""
        params = {"wxid": wxid}
        return requests.post(
            f"{self.base_url}/User/GetLoginStatus",
            params=params
        ).json()

    def user_get_user_info(self, wxid: str):
        """获取微信用户信息"""
        params = {"wxid": wxid}
        return requests.post(
            f"{self.base_url}/User/GetUserInfo",
            params=params
        ).json()

    def user_logout(self, wxid: str, force_logout: bool = False):
        """用户登出(强制退出)"""
        body = {
            "Wxid": wxid,
            "Force": 1 if force_logout else 0
        }
        return requests.post(
            f"{self.base_url}/User/Logout",
            json=body
        ).json()

    def user_send_log(self, wxid: str, log_level: int, log_data: str):
        """发送Log信息"""
        body = {
            "Wxid": wxid,
            "Type": log_level,
            "Log": log_data
        }
        return requests.post(
            f"{self.base_url}/User/SendLog",
            json=body
        ).json()

    def user_set_wechat_id(self, wxid: str, new_wechat_id: str):
        """设置微信ID(修改微信号)"""
        body = {
            "Wxid": wxid,
            "WechatId": new_wechat_id
        }
        return requests.post(
            f"{self.base_url}/User/SetWechatId",
            json=body
        ).json()

    def user_get_profile(self, wxid: str):
        """获取个人资料详情"""
        params = {"wxid": wxid}
        return requests.post(
            f"{self.base_url}/User/GetContractProfile",
            params=params
        ).json()

    def user_set_profile(self, wxid: str, nickname: str = None, gender: int = None, signature: str = None):
        """设置个人资料(修改个人信息)"""
        body = {"Wxid": wxid}
        if nickname:
            body["NickName"] = nickname
        if gender is not None:
            body["Gender"] = gender
        if signature:
            body["Signature"] = signature
            
        return requests.post(
            f"{self.base_url}/User/SetProfile",
            json=body
        ).json()

    def user_get_my_qr_code(self, wxid: str):
        """获取我的二维码"""
        params = {"wxid": wxid}
        return requests.post(
            f"{self.base_url}/User/GetMyQRCode",
            params=params
        ).json()

    def user_search_contact(self, wxid: str, keyword: str):
        """搜索联系人"""
        body = {
            "Wxid": wxid,
            "Keyword": keyword
        }
        return requests.post(
            f"{self.base_url}/User/SearchContact",
            json=body
        ).json()

    def user_get_contact_label(self, wxid: str):
        """获取联系人标签"""
        params = {"wxid": wxid}
        return requests.post(
            f"{self.base_url}/User/GetContactLabel",
            params=params
        ).json()

    def user_set_contact_label(self, wxid: str, contact_wxid: str, label_id: int):
        """设置联系人标签"""
        body = {
            "Wxid": wxid,
            "ContactWxid": contact_wxid,
            "LabelId": label_id
        }
        return requests.post(
            f"{self.base_url}/User/SetContactLabel",
            json=body
        ).json()

    def user_set_auto_download(self, wxid: str, enable: bool):
        """设置自动下载(媒体文件)"""
        body = {
            "Wxid": wxid,
            "Enable": 1 if enable else 0
        }
        return requests.post(
            f"{self.base_url}/User/SetAutoDownload",
            json=body
        ).json()

    def user_set_auto_reply(self, wxid: str, enable: bool, reply_content: str = ""):
        """设置自动回复"""
        body = {
            "Wxid": wxid,
            "Enable": 1 if enable else 0,
            "ReplyContent": reply_content
        }
        return requests.post(
            f"{self.base_url}/User/SetAutoReply",
            json=body
        ).json()

    def user_change_password(self, wxid: str, old_password: str, new_password: str):
        """修改密码"""
        body = {
            "Wxid": wxid,
            "OldPassword": old_password,
            "NewPassword": new_password
        }
        return requests.post(
            f"{self.base_url}/User/ChangePassword",
            json=body
        ).json()
